<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="../../assets/css/maincss.css" rel="stylesheet" />
<link href="../../assets/css/but.css" rel="stylesheet" />
</head>
<!-- Bootstrap -->
<link href="${pageContext.request.contextPath}/javascript/bootstrap/css/bootstrap.css" rel="stylesheet">
<link rel="stylesheet" href="${pageContext.request.contextPath}/javascript/table/bootstrap-table.css">
<script src="${pageContext.request.contextPath}/javascript/jQuery-1.12.4.js"></script>
<script src="${pageContext.request.contextPath}/javascript/bootstrap/js/bootstrap.js"></script>
<script src="${pageContext.request.contextPath}/javascript/table/bootstrap-table.js"></script>
<script src="${pageContext.request.contextPath}/javascript/table/locale/bootstrap-table-zh-CN.js"></script>
<body>
	<h3>类别管理</h3>
	<table  style="text-align: center;" >
		<tr>
			<td colspan="3" style="text-align: left;">
				<input type="text" id="currentPage" name="currentPage" value="1" hidden />
				<input type="text" id="pageSize" name="pageSize" value="5" hidden />
				<div class="form-inline">
					<input class="form-control" type="text" id="seachCateName" name="seachCateName" placeholder="请输入供应商名称" />
					<button class="btn btn-info" onclick="searchCategoryBut()" class="but green">查询</button>
				</div>
			</td>
			<td style="align: right;">
				<button class="btn btn-success" onclick="addCategoryBut()" class="but green">增 加</button>
			</td>
		</tr>
	</table>
	<table id="myTable" class="table table-hover" style="text-align: center;" >
		<tr>
			<td>类别id</td>
			<td>类别名称</td>
			<td>类别简介</td>
			<td>操作</td>
		</tr>
		
	</table>
	

</body>
<script type="text/javascript">

	/* //获取的json数据
	var jsonPage = null;
	// 总页数
	var totalPages = 0;
	
	//简化数据 获取
	function $(id) {
		return document.getElementById(id);
	}
	
	// 创建 xmlHttpRequest对象
	var xmlhttp;
	if (window.XMLHttpRequest){
	  xmlhttp=new XMLHttpRequest();
	} else {
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	
	
	// ajax 条件查询数据
	function searchCategoryBut(op){
		
		if(jsonPage != null && op == 1 && jsonPage.currentPage == 1){
			return 0;
		} else if(jsonPage != null && op == 2 && jsonPage.currentPage == totalPages){
			return 0;
		}
		
		
		var currentPage = $("currentPage").value;
		var pageSize = $("pageSize").value;
		var cateName = $("seachCateName").value;
		var table = $("myTable");
		var row = null;
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				var msg = xmlhttp.responseText;
				// alert(msg);
				jsonPage = JSON.parse(msg);
				var arr = jsonPage.list;
				
				// 跟新当前页..页大小
				$("currentPage").value = jsonPage.currentPage;
				$("pageSize").value = jsonPage.pageSize;
				
				// 计算总页数
				totalPages = parseInt((jsonPage.total + jsonPage.pageSize - 1) / jsonPage.pageSize);
				
				// 添加数据前,如果已经有查询的数据在table里,应该先清除表格数据
				var len = table.rows.length;
				for (var i = len - 1; i > 0; i--) {
					table.deleteRow(i);
				}
				
				// 将数据添加到表格中
				for(var i = 0;i < arr.length;i++){
					row = table.insertRow();
					row.insertCell().innerHTML = arr[i].categoryId;
					row.insertCell().innerHTML = arr[i].categoryName;
					row.insertCell().innerHTML = arr[i].categoryDesc;
					row.insertCell().innerHTML = 
						"<button onclick='editCategoryById("+arr[i].categoryId+")' class='but green'>修 改</button>"
						+"&nbsp;"
						+"<button onclick='return delCategoryById("+arr[i].categoryId+")' class='but red'>删 除</button>";
				}
				row = table.insertRow();
				row.innerHTML = 
					"<tr align='center'><td colspan='9'>"
					+"<a href='#' onclick='upPage()'>[上一页]</a>"
					+"&nbsp;&nbsp;&nbsp;&nbsp;"
					+"当前第"+jsonPage.currentPage+"页"
					+"&nbsp;&nbsp;"
					+"共"+jsonPage.total+"条数据"
					+"&nbsp;&nbsp;"
					+"共"+totalPages+"页"
					+"&nbsp;&nbsp;&nbsp;&nbsp;"
					+"<a href='#' onclick='nextPage()'>[下一页]</a>"
					+"</tr></td>";
				
			}
		}
		xmlhttp.open("GET", "CategoryServlet?op=doSearchCategory&currentPage="+currentPage
				+"&pageSize="+pageSize
				+"&cateName="+cateName,true);
		xmlhttp.send();
	}
	
	// 上一页
	function upPage(){
		if(jsonPage.currentPage > 1){
			$("currentPage").value = jsonPage.currentPage - 1;
		}
		searchCategoryBut(1);
	}
	
	// 下一页
	function nextPage(){
		if(jsonPage.currentPage < totalPages){
			$("currentPage").value = jsonPage.currentPage + 1;
		}
		searchCategoryBut(2);
	} */


	
	// 修改
	function editCategoryById(id){
		location.href = 'CategoryServlet?op=readyEditCategoryById&id='+id;
	}
	
	// 删除
	function delCategoryById(id){
		var msg = "确定删除该条数据?";
		if(confirm(msg) == true){
			location.href='CategoryServlet?op=removeCategoryById&id='+id;
			return true;
		} else {
			return false;	
		}
	}
	
	// 增加
	function addCategoryBut(){
		location.href='addCategory.jsp';
	}



</script>
</html>